<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\CustomModuleDataValueAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块数据值-业务类
 */
class CustomModuleDataValueBusiness extends BaseBusiness
{
        #[Inject]
        protected CustomModuleDataValueAccess $CustomModuleDataValueAccess;

        /**
         * 设置
         * @param mixed $customModuleDataId
         * @param array $inserts
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function set(mixed $customModuleDataId, array $inserts = []): array
        {
                $arrData = $this->CustomModuleDataValueAccess->forPage([
                        'wheres' => ['customModuleDataId' => $customModuleDataId], 'page' => 1, 'limit' => 500,
                        'fields' => ['customModuleDataValueId', 'fieldName', 'fieldValue'],
                ]);
                $this->CustomModuleDataValueAccess->delete(['wheres' => ['customModuleDataId' => $customModuleDataId]]);
                $arrBool = $this->CustomModuleDataValueAccess->batchInsert($inserts);
                $bool = true;
                foreach ($arrBool as $v) {
                        if (!$v) {
                                $bool = false;
                                break;
                        }
                }
                if (!$bool) return businessError();
                $this->createOperateLog($arrData, $inserts);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->CustomModuleDataValueAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 关联性删除
         * @param mixed|null $customModuleId
         * @param mixed|null $customModuleDataId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function relevanceDelete(mixed $customModuleId = null, mixed $customModuleDataId = null): array
        {
                $arrWheres = [];
                if ($customModuleId) $arrWheres['customModuleId'] = $customModuleId;
                if ($customModuleDataId) $arrWheres['customModuleDataId'] = $customModuleDataId;
                $arrData = $this->CustomModuleDataValueAccess->forPage([
                        'wheres' => $arrWheres, 'page' => 1, 'limit' => 10000,
                        'fields' => ['customModuleDataValueId', 'customModuleDataId', 'customModuleId', 'fieldName', 'fieldValue'],
                ]);
                $intEffectRow = $this->CustomModuleDataValueAccess->delete(['wheres' => $arrWheres]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }
}